//
//  ShoppingCenterViewController.m
//  成都咖啡
//
//  Created by rimi on 16/3/22.
//  Copyright © 2016年 wenhao. All rights reserved.
//

#import "ShoppingCenterViewController.h"
#import "DWFlowLayout.h"
#import "DWViewCell.h"
#import <BmobSDK/Bmob.h>
#import <UIImageView+WebCache.h>
#import "ShoppingName.h"

#define SCREENWITH   [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
@interface ShoppingCenterViewController (){
    NSArray *data;

}
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

- (void)initUserInterface;/**< 初始化用户界面 */
- (void)initDataSource;/**< 初始化数据源 */


@end

@implementation ShoppingCenterViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUserInterface];
    [self initDataSource];
//      [self.collectionView registerClass:[DWViewCell class] forCellWithReuseIdentifier:@"DWViewCell"];
  
    
   
}
#pragma mark - Init methods

- (void)initDataSource{
    ///拿到属于这个cell分类的shop数组
    BmobQuery *query = [BmobQuery queryWithClassName:@"ShopInfo"];
    //在分类约束下的值：在ShopInfo表里，shopTag里面不同tag值的数组bquery
   
    [query whereKey:@"shopTag" equalTo:_shopListString];
  
    //查找GameScore表的数据
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"无法获取数据，请检查网络(☆_☆)" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
            [alertView show];

            
        } else {
            data = array;
            for (BmobObject *obj in array) {
               NSLog(@"###%@",obj);
            }
            [self.collectionView reloadData];
        }
    
       
        
    }];
   
}
- (void)initUserInterface{
    
    DWFlowLayout *layout =  [[DWFlowLayout alloc] init];
    self.collectionView.collectionViewLayout = layout;
  
}

#pragma mark cell的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return data.count;
}

#pragma mark cell的视图
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"DWViewCell";
    DWViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    NSInteger row = indexPath.row;
    
    //FIXME: 现在这个data的值是数组里面有字典，所以要改得到方法
    //这里不用。到image，直接用下面的方法
//    [cell.shopPicture sd_setImageWithURL:[NSURL URLWithString:[[data objectAtIndex:row] objectForKey:@"shop_picture"]]];
    
    
    UIImage *image = [UIImage imageNamed:@"kb"];
    NSURL *imageUrl = [NSURL URLWithString:[[data objectAtIndex:row] objectForKey:@"shop_picture"]];
    [cell.shopPicture sd_setImageWithURL:imageUrl placeholderImage:image];

    //！！！!!!:取这个必须用objectforkey，不能用valueForKey。
    cell.shopName.text = [[data objectAtIndex:row] objectForKey:@"shopName"];
    
    [ShoppingName shareShoppingName].selectedTitle = cell.shopName.text;
    //下个页面所有内容的来源判断标准
    [ShoppingName shareShoppingName].selectedObjectID = [[data objectAtIndex:row] objectForKey:@"objectId"];
    
    
    cell.shopAddress.text = [[data objectAtIndex:row] objectForKey:@"shopAddress"];
    
    return cell;
    
}

#pragma mark cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREENWITH - 60, SCREENHEIGHT - 64 - 60 - 60);
}

#pragma mark cell的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"点击图片%ld",indexPath.row);
    [self performSegueWithIdentifier: @"DrawerIdentifier" sender:self];
}


@end
